package com.asiainfo.integration.tibco;

import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Properties;

public class TibJmsQueueSender {

    //初始化上下文
    private static final String INITIAL_CONTEXT_FACTORY = "com.tibco.tibjms.naming.TibjmsInitialContextFactory";

    //EMS服务器的地址
    private static final String JMS_NAMING = "tibjmsnaming://localhost:7222";//指定发送消息的Queue的名称

    private static final String QUEUE_NAME = "veris.test.queue";//EMS用户名

    private static final String SECURITY_PRINCIPAL = "veris";//EMS密码

    private static final String SECURITY_CREDENTIALS = "veris";//当前会话

    private static final String queueConnectionFactory = "VerisQueueConnectionFactory";

    private static QueueSession session = null;//EMS连接

    private static QueueConnection connection = null;//发送消息的Queue

    private static Queue queue = null;

    static {
        try {
            Properties tibcoProperties = new Properties();
            tibcoProperties.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
            tibcoProperties.put(Context.PROVIDER_URL, JMS_NAMING);
            tibcoProperties.put(Context.SECURITY_PRINCIPAL, SECURITY_PRINCIPAL);
            tibcoProperties.put(Context.SECURITY_CREDENTIALS, SECURITY_CREDENTIALS);
            Context context = new InitialContext(tibcoProperties);
            QueueConnectionFactory factory = (QueueConnectionFactory) context.lookupLink(queueConnectionFactory);
            connection = factory.createQueueConnection(SECURITY_PRINCIPAL, SECURITY_CREDENTIALS);
            queue = (Queue) context.lookup(QUEUE_NAME);
            session = connection.createQueueSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
        } catch (NamingException e) {
            e.printStackTrace();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    /**
     * 测试发送消息*@paramargs
     */
    public static void main(String args[]) throws NamingException, JMSException {
        if (connection == null || queue == null || session == null) {
            return;
        }
        QueueSender sender = session.createSender(queue);
        TextMessage message = session.createTextMessage();

        message.setText("This is a sender message by Charkey.");
        sender.send(message);
        QueueReceiver receiver = session.createReceiver(queue);
        Message receiverMessage = receiver.receive(1000);
        if (receiverMessage == null) {
            System.out.println("No reply message retrieved");
        } else {
            String replyString = null;
            if (receiverMessage instanceof TextMessage) {
                replyString = ((TextMessage) receiverMessage).getText();
            } else if (receiverMessage instanceof BytesMessage) {
                replyString = receiverMessage.toString();
            }
            System.out.println("replyString:" + replyString);
        }
        receiver.close();
        sender.close();
    }
}